#include "messionmid.h"
#include<QVBoxLayout>
#include<QScrollBar>
#include<QPushButton>
#include<QStyleOption>
#include<QLabel>
#include"debug.h"
#include<QPainter>
#include"model/data.h"
#include"mainWidget.h"
messionMid::messionMid(QWidget *parent)
    :  QScrollArea{parent}
{
    this->setStyleSheet("QWidget{background-color:rgb(231,231,231)}");
    //设置垂直滚动条

    this->setWidgetResizable(true);
    this->verticalScrollBar()->setStyleSheet("QScrollBar:vertial{width:1px;background-color:rgb(64,64,64);}");
    this->horizontalScrollBar()->setStyleSheet("QScrollBar:horizontal{height:0px;}");

    container=new QWidget();
    this->setWidget(container);
    QVBoxLayout *layout=new QVBoxLayout();
    layout->setSpacing(0);
    layout->setContentsMargins(0,20,0,0);
    layout->setAlignment(Qt::AlignTop);
    container->setLayout(layout);
#if TEST_UI

    for(int i =0;i<30;i++)
    {

        this->addItem(FRIENDTYPE,QString::number(i),QIcon(":/resource/Image/useravatar1.png"),
                      "张三"+QString::number(i),""+QString::number(i));
    }

#endif
}



void messionMid::clear()
{
    QLayout*layout= this->layout();
    for(int i=layout->count();i>=0;--i)
    {
        //首先移除元素
      QLayoutItem*item=layout->takeAt(i);
        //不仅要移除，还要删除
        if(item->widget())
      {
          delete item->widget();
        }
    }
}

void messionMid::addItem(ITEMTYPE type,const QString&id,const QIcon&avatar,const QString&name,const QString&text)
{
    ssionFriendItem*item=nullptr;
    //这里在要处理不同类型的Item的添加
    if(type==SSIONTYPE)
    {
       item=new SsionItem(this,id,avatar,name,text);
        container->layout()->addWidget(item);
    }
    else if(type==FRIENDTYPE)
    {
        item=new FriendItem(this,id,avatar,name,text);
        container->layout()->addWidget(item);
    }
    else if(type==APPLYTYPE)
    {
         item=new ApplyItem(this,id,avatar,name);
        container->layout()->addWidget(item);
    }
    else
    {
        LOG()<<"类型错误";
        return;
    }
}

void messionMid::clicked(int index)
{
    if(index<0||index>container->layout()->count())
    {
        LOG()<<"下标超过合法范围";
        return;
    }
    if(container->layout()->itemAt(index)==nullptr||container->layout()->itemAt(index)->widget()==nullptr)
    {
        LOG()<<"该下标对应的值非法";
        return;
    }
    //把取到的item强转
   ssionFriendItem*item= dynamic_cast<ssionFriendItem*>(container->layout()->itemAt(index)->widget());
    item->select();
}



////////////////////////////////////
/// 实现一个好友会话项目类（包含名字，头像，最后一条对话（或个性签名））
////////////////////////////////////
ssionFriendItem::ssionFriendItem( QWidget *owner,const  QIcon &avatar, const QString &name, const QString &text)
    :owner(owner)
{
    this->setFixedHeight(70);
    this->setStyleSheet("QWidget { background-color: rgb(231, 231, 231); }");
    //设置整个大的框架
        setStyleSheet("QWidget{background-color:rgb(231,231,231)}");
    //设置网格布局管理器将我们的元素放置进去
    QGridLayout *layout=new  QGridLayout();
    layout->setHorizontalSpacing(10);
    layout->setVerticalSpacing(0);
    layout->setContentsMargins(20,0,0,0);
     this->setLayout(layout);
    //头像类的设置
    QPushButton* avatarBtn=new QPushButton();
    avatarBtn->setFixedSize(50,50);
    avatarBtn->setIcon(avatar);
    avatarBtn->setIconSize(QSize(50,50));
    avatarBtn->setSizePolicy(QSizePolicy::Fixed,QSizePolicy::Fixed);
    avatarBtn->setStyleSheet("QPushButton{border:none;}");
    //名字类的设置
    QLabel* userName=new QLabel();
   userName->setText(name);
    userName->setFixedHeight(35);
   userName->setStyleSheet("QLabel{font-size:18px;font-width:600;}");
    userName->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Fixed);
    //最后一条对话（或个性签名）设置
     messageOrdescription=new QLabel();
     messageOrdescription->setText(text);
     messageOrdescription->setFixedHeight(35);
     messageOrdescription->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Fixed);
    //将我们的元素放到网格布局管理器中
    layout->addWidget(avatarBtn,0,0,2,2);
    layout->addWidget(userName,0,2,1,8);
    layout->addWidget( messageOrdescription,1,2,1,8);

}

void ssionFriendItem::mousePressEvent(QMouseEvent *event)
{
    (void)event;
    select();
   }

void ssionFriendItem::paintEvent(QPaintEvent *event)
{
    (void)event;
    QStyleOption opt;
    opt.initFrom(this);
    QPainter p(this);
    style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
}

void ssionFriendItem::select()
{
    //先去获取所有的item
    const QObjectList children= this->parentWidget()->children();
    //迭代器遍历
    for(QObject * child:children)
    {
        //如果不是widget
        if(!child->isWidgetType())
        {
            continue;
        }
        //如果是的话,强转
        ssionFriendItem* item=dynamic_cast<ssionFriendItem*>(child);
        if (item->selected) {
            item->selected = false;
            item->setStyleSheet("QWidget { background-color: rgb(231, 231, 231); }");
        }
    }
    this->setStyleSheet("QWidget{background-color:rgb(210,210,210);}");
    this->selected=true;
    this->active();
}

void ssionFriendItem::enterEvent(QEnterEvent*event)
{
    this->setMouseTracking(true);
    (void)event;
    if(this->selected)
    {
        return;
    }
     this->setStyleSheet("QWidget{background-color:rgb(215,215,215);}");
}

void ssionFriendItem::leaveEvent(QEvent*event)
{
    (void) event;

    // 当前这个 item 是选中状态, 则背景色不受到该逻辑影响
    if (this->selected) {
        return;
    }
    // 还原背景色
    this->setStyleSheet("QWidget { background-color: rgb(231, 231, 231);}");
}

void ssionFriendItem::active()
{
    //在父类里面，他不需要实现任何逻辑
    return;

}
SsionItem::SsionItem(QWidget *owner, const QString &id, const QIcon &avatar, const QString &name, const QString &lastmessage)
    :ssionFriendItem(owner,avatar,name,lastmessage),chatMessionId(id)
{
}

void SsionItem::active()
{
    //之后补充
    return;
}

FriendItem::FriendItem(QWidget *owner, const QString &id, const QIcon &avatar, const QString &name, const QString &description)
:ssionFriendItem(owner,avatar,name,description),userId(id)
{

}

void FriendItem::active()
{
    // 点击之后, 要激活对应的会话列表元素
   // LOG() << "点击 FriendItem 触发的逻辑! userId=" << userId;

    // MainWidget* mainWidget = MainWidget::getInstance();
    // //mainWidget->MessionTableSwitch(userId);
    return;
}

ApplyItem::ApplyItem(QWidget *owner, const QString&userid,const QIcon &avatar, const QString &name)
    :ssionFriendItem(owner,avatar,name,""),userId(userid)
{
    //想办法把父类的messageOrdescription移除
    //先获取我们的布局管理器
  QGridLayout*layout=dynamic_cast<QGridLayout*>(this->layout());
  layout->removeWidget(messageOrdescription);
  delete messageOrdescription;

  //在向里面添加同意与拒绝两个按钮
  QPushButton*acceptBtn=new QPushButton();
  QPushButton*rejectBtn=new QPushButton();
  acceptBtn->setText("同意");
  rejectBtn->setText("拒绝");
  layout->addWidget(acceptBtn,1,2,1,1);
  layout->addWidget(rejectBtn,1,3,1,1);
  acceptBtn->setStyleSheet("QPushButton{background-color:transparent;}");
   rejectBtn->setStyleSheet("QPushButton{background-color:transparent;}");

}

void ApplyItem::active()
{
    return;
}
